package br.senac.sp.persistence;

import java.util.HashMap;
import java.util.List;

public class PersistenceFacade {

    private static PersistenceFacade instance;

    private IConnector connector;

    public static PersistenceFacade getInstance() {
        if(instance == null) {
            instance = new PersistenceFacade();
        }
        return instance;
    }
    
    private HashMap<Class, IMapper> mappers;
    
    private PersistenceFacade() {
        mappers = new HashMap<Class, IMapper>();
    }
    
    public <Entity, OID> Entity get(OID id, Class<Entity> entityType) {
        IMapper<Entity, OID> mapper = mappers.get(entityType);
        return mapper.get(id);
    }
    
    public <Entity, OID> List<Entity> get(Class<Entity> entityType) {
        IMapper<Entity, OID> mapper = mappers.get(entityType);
        return mapper.get();
    }
    
    public <Entity, OID> void put(Entity entity) {
        IMapper<Entity, OID> mapper = mappers.get(entity.getClass());
        mapper.put(entity);
    }
    
    public <Entity, OID> void remove(Entity entity) {
        IMapper<Entity, OID> mapper = mappers.get(entity.getClass());
        mapper.remove(entity);
    }

    public IConnector getConnector() {
        return connector;
    }

    public <Entity> PersistenceFacade registerMapper(Class<Entity> entityType, IMapper<Entity,?> mapper) {
        mappers.put(entityType, mapper);
        return this;
    }

    public PersistenceFacade useConnector(IConnector connector) {
        this.connector = connector;
        return this;
    }
}
